﻿namespace Pub.Class
{
    using System;
    using System.IO;
    using System.Net;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Web;
    using System.Web.UI;

    public class Net
    {
        private static readonly object lockHelper = new object();

        public static string DownFile(string url, string fileName)
        {
            using (WebClient client = new WebClient())
            {
                client.DownloadFile(url, fileName);
            }
            return fileName;
        }

        public bool GetHttpFile(string sUrl, string sSavePath)
        {
            bool flag = false;
            WebResponse response = null;
            WebRequest request = WebRequest.Create(sUrl);
            request.Timeout = 0xc350;
            try
            {
                response = request.GetResponse();
            }
            catch (WebException exception)
            {
                exception.Message.ToString();
            }
            catch (Exception exception2)
            {
                exception2.ToString();
            }
            finally
            {
                if (response.IsNotNull())
                {
                    BinaryReader reader = new BinaryReader(response.GetResponseStream(), Encoding.GetEncoding("GB2312"));
                    int count = Convert.ToInt32(response.ContentLength);
                    try
                    {
                        FileStream stream;
                        if (System.IO.File.Exists(HttpContext.Current.Request.MapPath("RecievedData.tmp")))
                        {
                            stream = System.IO.File.OpenWrite(sSavePath);
                        }
                        else
                        {
                            stream = System.IO.File.Create(sSavePath);
                        }
                        stream.SetLength((long) count);
                        stream.Write(reader.ReadBytes(count), 0, count);
                        stream.Close();
                    }
                    finally
                    {
                        reader.Close();
                        response.Close();
                    }
                    flag = true;
                }
            }
            return flag;
        }

        public static string GetRemoteHtmlCode(string Url)
        {
            string str2;
            lock (lockHelper)
            {
                Url = Url + ((Url.IndexOf("?") >= 0) ? "&time=" : "?time=") + Rand.RndDateStr();
                using (WebClient client = new WebClient())
                {
                    client.Credentials = CredentialCache.DefaultCredentials;
                    byte[] bytes = client.DownloadData(Url);
                    str2 = bytes.GetHtmlEncoding(Encoding.UTF8).GetString(bytes);
                }
            }
            return str2;
        }

        public static string GetRemoteHtmlCode2(string Url)
        {
            return GetRemoteHtmlCode2(Url, Encoding.Default);
        }

        public static string GetRemoteHtmlCode2(string Url, Encoding encoding)
        {
            lock (lockHelper)
            {
                Url = Url + ((Url.IndexOf("?") >= 0) ? "&time=" : "?time=") + Rand.RndDateStr();
                string str = "";
                HttpWebResponse response = null;
                StreamReader reader = null;
                try
                {
                    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(Url);
                    response = (HttpWebResponse) request.GetResponse();
                    reader = new StreamReader(response.GetResponseStream(), encoding);
                    str = reader.ReadToEnd();
                }
                catch
                {
                }
                finally
                {
                    if (reader.IsNotNull())
                    {
                        reader.Close();
                    }
                    if (response.IsNotNull())
                    {
                        response.Close();
                    }
                }
                return str;
            }
        }

        public static string GetRemoteHtmlCode3(string strUrl)
        {
            return GetRemoteHtmlCode3(strUrl, Encoding.Default);
        }

        public static string GetRemoteHtmlCode3(string strUrl, Encoding encoding)
        {
            lock (lockHelper)
            {
                strUrl = strUrl + ((strUrl.IndexOf("?") >= 0) ? "&time=" : "?time=") + Rand.RndDateStr();
                StringBuilder builder = new StringBuilder();
                HttpWebResponse response = null;
                StreamReader reader = null;
                Stream responseStream = null;
                try
                {
                    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(strUrl);
                    request.AllowAutoRedirect = true;
                    request.Timeout = 0x2bf20;
                    request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
                    request.Referer = strUrl;
                    response = (HttpWebResponse) request.GetResponse();
                    responseStream = response.GetResponseStream();
                    reader = new StreamReader(responseStream, encoding);
                    while (reader.Peek() != -1)
                    {
                        builder.Append(reader.ReadToEnd());
                    }
                }
                catch
                {
                    return "-1";
                }
                finally
                {
                    if (reader.IsNotNull())
                    {
                        reader.Close();
                    }
                    if (responseStream.IsNotNull())
                    {
                        responseStream.Close();
                    }
                    if (response.IsNotNull())
                    {
                        response.Close();
                    }
                }
                return builder.ToString();
            }
        }

        public static string GetRemoteHtmlCode4(string strUrl)
        {
            return GetRemoteHtmlCode4(strUrl, Encoding.Default);
        }

        public static string GetRemoteHtmlCode4(string Url, Encoding encoding)
        {
            string str2;
            lock (lockHelper)
            {
                Url = Url + ((Url.IndexOf("?") >= 0) ? "&time=" : "?time=") + Rand.RndDateStr();
                using (WebClient client = new WebClient())
                {
                    client.Credentials = CredentialCache.DefaultCredentials;
                    byte[] bytes = client.DownloadData(Url);
                    str2 = encoding.GetString(bytes);
                }
            }
            return str2;
        }

        public static string GetRemoteHtmlCode5(string strUrl)
        {
            return GetRemoteHtmlCode5(strUrl, Encoding.Default);
        }

        public static string GetRemoteHtmlCode5(string Url, Encoding encoding)
        {
            string str3;
            lock (lockHelper)
            {
                Url = Url + ((Url.IndexOf("?") >= 0) ? "&time=" : "?time=") + Rand.RndDateStr();
                string s = "";
                using (WebClient client = new WebClient())
                {
                    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    byte[] bytes = Encoding.ASCII.GetBytes(s);
                    byte[] buffer2 = client.UploadData(Url, "POST", bytes);
                    str3 = encoding.GetString(buffer2);
                }
            }
            return str3;
        }

        public static bool ResponseFile(string fileFile, string newFileName = "")
        {
            if (!System.IO.File.Exists(fileFile))
            {
                return false;
            }
            FileInfo info = new FileInfo(fileFile);
            newFileName = string.IsNullOrEmpty(newFileName) ? info.FullName : newFileName;
            if (Request2.GetBrowser().ToLower().IndexOf("ie") != -1)
            {
                newFileName = HttpUtility.UrlEncode(newFileName, Encoding.UTF8);
            }
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = false;
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + newFileName);
            HttpContext.Current.Response.AppendHeader("Content-Length", info.Length.ToString());
            HttpContext.Current.Response.WriteFile(info.FullName);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            return true;
        }

        public static void ResponseFile(string filepath, string filename, string filetype)
        {
            byte[] buffer = new byte[0x2710];
            using (Stream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                long length = stream.Length;
                HttpContext.Current.Response.ContentType = filetype;
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename.Trim().UrlEncode().Replace("+", " "));
                while (length > 0L)
                {
                    if (HttpContext.Current.Response.IsClientConnected)
                    {
                        int count = stream.Read(buffer, 0, 0x2710);
                        HttpContext.Current.Response.OutputStream.Write(buffer, 0, count);
                        HttpContext.Current.Response.Flush();
                        buffer = new byte[0x2710];
                        length -= count;
                    }
                    else
                    {
                        length = -1L;
                    }
                }
            }
            HttpContext.Current.Response.End();
        }

        public static void TransHtml(string path, string outpath, Encoding encoding)
        {
            Page page = new Page();
            StringWriter writer = new StringWriter();
            page.Server.Execute(path, writer);
            outpath = (outpath.IndexOf(@"\") > 0) ? outpath : outpath.GetMapPath();
            FileDirectory.FileDelete(outpath);
            Log.Write(outpath, writer.ToString(), encoding);
        }
    }
}

